/**
 * 右侧联系人操作
 * by yangfan
 * 样式改变较大，重写
 */
fangimjs.define('view/contactListView', [
    'jquery',
    'event/event',
    'setting/setting'
], function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        event = require('event/event'),
        vars = fangimjs.data.vars,
        setting = require('setting/setting').getInstance();

    function contactListView() {
        this.rightCon='#chatW_rt';//右部区域总id
        this.contactsTotalCon = '.conscroll'; //联系人总容器id
        this.chatWContact1 = '#chatW_rt_contact'; //最近联系人容器id
        this.chatWContact2 = '#chatW_rt_contact_2'; //置业顾问列表容器
        this.chatWContact3 = '#chatW_rt_contact_3'; //本地经纪人列表容器
        this.chatWContact4 = '#chatW_rt_contact_4'; //在线客服列表容器
        var commonContactText = '<div class="conts"><em class="on"></em><a href="javascript:void(0);">*</a><span class="news" style="display:none;"></span></div>'; //通用显示列表h3头
        this.contact1Text = commonContactText.replace('*', '\u6700\u8fd1\u8054\u7cfb\u4eba');//最近联系人
        this.contact2Text = commonContactText.replace('*', '\u672c\u697c\u76d8\u7f6e\u4e1a\u987e\u95ee');//本楼盘置业顾问
        this.contact3Text = commonContactText.replace('*', '\u672c\u5730\u7ecf\u7eaa\u4eba');//本地经纪人
        this.contact4Text = commonContactText.replace('*', '\u5728\u7ebf\u5ba2\u670d');//在线客服
        this.contactH3Class = 'on'; //列表H3头收起展开样式类名 .conts > em
        this.currentClass = 'on'; //当前联系人样式类名
        this.onlineClass = ''; //在线联系人样式类名 ul.rlist li
        this.notOnlineClass = 'no2'; //不在线联系人样式类名 ul.rlist li.no2
        this.winClass = 'max'; //列表ul样式类名，注：用于当左侧聊天框关闭时使当前联系人样式消失使用
        this.listCon = '<ul class="rlist"></ul>'; //展示联系人列表容器
        this.chatRB = "#chatW_rb"; //联系人按钮id
        this.chatRBNum1 = "#chatW_rb_num1"; //联系人按钮在线人数数量id
        this.chatRBNum2 = "#chatW_rb_num2"; //联系人按钮总人数数量id
        this.chatRTUNum1 = "#chatW_rt_up_num1"; //联系人按钮在线人数数量id
        this.chatRTUNum2 = "#chatW_rt_up_num2"; //联系人按钮总人数数量id
    };

    contactListView.prototype = {
        /**
         * 初始化函数
         */
        init: function () {
            var that = this;
            that.chatBox = vars.chatBox;
            that.chatBoxMain = vars.chatBoxMain;
            that.chatBoxDetail = vars.chatBoxDetail
            that.chatBoxNewMsg = vars.chatBoxNewMsg;
            // that.jOnlineTip = that.chatBoxMain.find('div.title .floatl');
            that.conScroll = that.chatBoxMain.find(that.contactsTotalCon)
                .on('click', 'li', function () {
                    that.selectContactByClick(this);
                })
                .on('click', '.conts', function () {
                    that.showOrHideLiset(this);
                });
            that.jContact1 = that.conScroll.find(that.chatWContact1).append(that.contact1Text + that.listCon);
            that.onlineTalk = that.chatBoxMain.find('.btnOnline').on('click', function() {
                event.trigger('view:window:change', setting.WINDOW_STATUS_TINY);
            });
        },
        /**
         * 选择单个联系人
         * type 联系人类型（最近联系人、在线客服等）
         */
        selectContactByClick: function (t) {
            var that = this;
            var $this = $(t);
            var contactElID = '#' + $this.closest('div').attr('id');
            if (contactElID == that.chatWContact1) {
                event.trigger('view:contact:selected', $this.attr('id'));
            } else {
                var type = '';
                switch (contactElID){
                    case that.chatWContact2:
                        type = setting.CONTACT_CONSULTANT;
                        break;
                    case that.chatWContact3:
                        type = setting.CONTACT_BROKER;
                        break;
                    case that.chatWContact4:
                        type = setting.CUSTOMER_SERVICE;
                        break;
                }
                event.trigger('view:otherContact:selected', {data: $this.attr('id'), type: type});
            }
            that.conScroll.scrollTop(0);
        },
        /**
         * 选择联系人
         * @param {Object} data 当前选择的联系人信息
         */
        selectContact: function (data) {
            var that = this;
            var ulCldArr = that.jContact1.find('ul').children();
            ulCldArr.removeClass(that.currentClass);
            var l = ulCldArr.length;
            for (var i = 0; i < l; i++) {
                var cldID = $(ulCldArr[i]);
                if (cldID.attr('id') == data.contactId) {
                    var onlineClass=data.state != 1 ? that.notOnlineClass : that.onlineClass;
                    var onlineText=data.state != 1 ? '不在线' : '在线';//\u4e0d\u5728\u7ebf:
                    cldID.removeClass().addClass([onlineClass,that.currentClass].join(' '));
                    cldID.find('.news').addClass('nonews').html('');
                    break;
                }
            }
        },
        /**
         * 改变联系人按钮显示联系人数量
         * @param {Object} ol 在线人数
         * @param {Object} all 总人数
         */
        changeContactNum: function (obj) {
            var that = this;
            if(undefined === that.jContactOnline){
                that.jContactOnline = that.onlineTalk.find('span:first'); // .add(that.chatBoxMainUp.find(that.chatRTUNum1));
            }
            if(undefined === that.jContactTotal){
                that.jContactTotal = that.onlineTalk.find('span:last'); // .add(that.chatBoxMainUp.find(that.chatRTUNum2));
            }
            that.jContactOnline.html(obj.online.toString());
            that.jContactTotal.html(obj.total.toString());
        },
        /**
         * 更新单个联系人新消息数据
         * @param {Object} data
         */
        updateNewMsg: function (data) {
            var that = this;
            var totalMsg = data.totalMsg || 0;
            if(undefined === that.jContactNewMsgNum){
                that.jContactNewMsgNum = that.jContact1.find('.conts .news');
            }
            that.jContactNewMsgNum.html((totalMsg > 0 ? totalMsg:''));
            var newMsgContact = data.contactData;
            if (!newMsgContact)return;
            var ulCldArr = that.jContact1.find('ul').children();
            var l = ulCldArr.length;
            for (var i = 0; i < l; i++) {
                var cldID = $(ulCldArr[i]);
                if (cldID.attr('id') == newMsgContact.contactId) {
                    newMsgContact.newmsgnum > 0 ? cldID.find('.news').removeClass('nonews').html(newMsgContact.newmsgnum) : cldID.find('.news').removeClass('red');
                    break;
                }
            }
        },
        /**
         * 展示或隐藏列表
         */
        showOrHideLiset: function (t) {
            var that = this;
            var $this = $(t);
            var $em = $this.find('em');
            if ($em.hasClass(that.contactH3Class)) {
                $em.removeClass(that.contactH3Class);
            } else {
                $em.addClass(that.contactH3Class);
            }
            $this.next().toggle();
        },
        toggleBoxMain: function() {
            var that = this;
            if(that.chatBoxBar.is(':visible')) {
                that.chatBoxMain.show();
                that.chatBoxBar.hide();
            } else {
                that.chatBoxMain.hide();
                that.chatBoxBar.show();
            }
        },
        /**
         * 根据窗口状态通过class名改变当前选中联系人的显示
         * ?????
         */
        updateWinShow: function (obj) {
            var that = this;
            var ulEl = that.jContact1.find('ul');
            if (obj.boxState == setting.WINDOW_STATUS_MAIN) {
                ulEl.removeClass(that.winClass);
            } else {
                ulEl.addClass(that.winClass);
            }
        },
        /**
         *  更新联系人列表（包括最近联系人、置业顾问和本地经纪人）
         * @param {Object} obj
         * @param {Object} type 联系人类型
         */
        updateContact: function (obj, type) {
            var that = this;
            var t = type ? type : setting.CONTACT_RECENT;
            var contactElID = that.chatWContact1;
            if (t != setting.CONTACT_RECENT) {
                var contactText='';
                switch (t){
                    case setting.CONTACT_CONSULTANT:
                        contactElID=that.chatWContact2;
                        contactText=that.contact2Text;
                        break;
                    case setting.CONTACT_BROKER:
                        contactElID=that.chatWContact3;
                        contactText=that.contact3Text;
                        break;
                    case setting.CUSTOMER_SERVICE:
                        contactElID=that.chatWContact4;
                        contactText=that.contact4Text;
                        break;
                }
                $(contactElID).empty().append(contactText + that.listCon);
            }
            var ulEl = $(contactElID).find('ul');
            var list = obj;
            ulEl.empty();
            var htmlArr = [];
            var len = list.length;
            var newMsgNum = 0;
            for (var i = 0; i < len; i++) {
                htmlArr.push('<li id="');
                htmlArr.push(list[i].contactId);
                htmlArr.push('" title="');
                htmlArr.push(list[i].showname);
                htmlArr.push('" class="');
                htmlArr.push(list[i].state != 1 ? this.notOnlineClass : this.onlineClass);
                htmlArr.push('"><span class="talk_ft">');
                htmlArr.push((list[i].listname||list[i].showname));
                htmlArr.push('</span><span class="news nonews');
                htmlArr.push(+list[i].newmsgnum > 0 ? ' red">' + list[i].newmsgnum : '">');
                htmlArr.push('</span></li>');
                newMsgNum += +list[i].newmsgnum;
            }
            ulEl.html(htmlArr.join(''));
            if(contactElID == that.chatWContact1){
                if(undefined === that.jContactNewMsgNum){
                    that.jContactNewMsgNum = that.jContact1.find('.conts .news');
                }
                that.jContactNewMsgNum.removeClass('nonews').html(newMsgNum>0?newMsgNum:'');
            }
            that.conScroll.scrollTop(0);
            if (contactElID === that.chatWContact1 && len>0 && vars.config.showIcon) {
                $.isFunction(vars.config.showIcon) && vars.config.showIcon();
                vars.config.showIcon = null;
            }

        }
    };
    module.exports = new contactListView;
});
